﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Business;

public partial class Unsubscribe : System.Web.UI.Page {
    protected void Page_Load(object sender, EventArgs e) {
        CancelButton.Attributes.Add("onclick", "window.close()");
    }
    protected void CancelButton_Click(object sender, EventArgs e) {
        Response.Write("<script language='javascript'> { window.close();}</script>");
    }
    protected void UnsubscribeButton_Click(object sender, EventArgs e) {
        string status = "Deactivated";
        string email = "";
        string subject = "i2iPortal Unsubscribe Confirmation";
        string strMsg = "Thank you for all this time. You have been successfully unsubscribe from i2iPortal";

        try {
            CreateConn objcon = new CreateConn();
            string query = "SELECT email FROM Subscribers WHERE email = '" + emailTextBox.Text + "'";
            DataTable dt = objcon.SQLConnDataTable(query);
            email = dt.Rows[0]["email"].ToString();

            try {
                string updateQuery = "UPDATE Subscribers SET dateAltered = '" + DateTime.Now + "',status = '" + status + "'" +
                "WHERE  (email = '" + email + "')";
                objcon.SQLCmd(query);
                Email objmail = new Email();
                if (!objmail.SendEmail(email, subject, strMsg)) {
                    FailureText.Text = "please try again...";
                    FailureText.Visible = true;
                } else {
                    FailureText.Text = "Thank you for your interest";
                    FailureText.Visible = true;
                    emailTextBox.Text = String.Empty;
                }
            } catch (Exception err) {
                Console.Write("Err msg:" + err.Message);
            }
        } catch (Exception err) {
            Console.Write("Err msg:" + err.Message);
            FailureText.Text = "we don't recognise your email or you have been unsubscribe before";
            FailureText.Visible = true;
        }
    }
}
